// 接码平台接口定义
interface IConnectCodePlatform {
  /**
   * 获取手机号码
   * @param projectId 项目ID
   * @returns 可用的手机号码
   */
  getPhone(projectId: string): Promise<string>;

  /**
   * 获取短信内容
   * @param phone 手机号码
   * @param projectId 项目ID
   * @returns 短信验证码内容
   */
  getSmsContent(phone: string, projectId: string): Promise<string>;

  /**
   * 释放手机号码
   * @param phone 手机号码
   * @param projectId 项目ID
   */
  releasePhone(phone: string, projectId: string): Promise<void>;

  /**
   * 加黑手机号码
   * @param phone 手机号码
   * @param projectId 项目ID
   */
  addBlacklist(phone: string, projectId: string): Promise<void>;
}

// 平台配置
interface PlatformConfig {
  name: string;
  baseUrl: string;
  token: string;
  [key: string]: any; // 其他平台特定配置
}

// 接码平台基类
class ConnectCodeBase implements IConnectCodePlatform {
  protected config: PlatformConfig;

  constructor(config: PlatformConfig) {
    this.config = config;
  }

  async getPhone(projectId: string): Promise<string> {
    throw new Error('Method not implemented.');
  }

  async getSmsContent(phone: string, projectId: string): Promise<string> {
    throw new Error('Method not implemented.');
  }

  async releasePhone(phone: string, projectId: string): Promise<void> {
    throw new Error('Method not implemented.');
  }

  async addBlacklist(phone: string, projectId: string): Promise<void> {
    throw new Error('Method not implemented.');
  }
}

// 示例：某接码平台实现
class ExamplePlatform extends ConnectCodeBase {
  async getPhone(projectId: string): Promise<string> {
    // 实现获取手机号逻辑
    // const response = await fetch(`${this.config.baseUrl}/getPhone?token=${this.config.token}&projectId=${projectId}`);
    // return response.json();
    return '';
  }

  async getSmsContent(phone: string, projectId: string): Promise<string> {
    // 实现获取短信内容逻辑
    // const response = await fetch(`${this.config.baseUrl}/getSms?token=${this.config.token}&phone=${phone}&projectId=${projectId}`);
    // return response.json();
    return '';
  }

  async releasePhone(phone: string, projectId: string): Promise<void> {
    // 实现实释放手机号逻辑
    // await fetch(`${this.config.baseUrl}/releasePhone?token=${this.config.token}&phone=${phone}&projectId=${projectId}`);
  }

  async addBlacklist(phone: string, projectId: string): Promise<void> {
    // 实现加黑手机号逻辑
    // await fetch(`${this.config.baseUrl}/addBlacklist?token=${this.config.token}&phone=${phone}&projectId=${projectId}`);
  }
}

// 接码平台管理器
class ConnectCode {
  private platforms: Map<string, IConnectCodePlatform> = new Map();
  private currentPlatform: string | null = null;

  /**
   * 注册平台
   * @param name 平台名称
   * @param platform 平台实例
   */
  registerPlatform(name: string, platform: IConnectCodePlatform): void {
    this.platforms.set(name, platform);
  }

  /**
   * 设置当前使用的平台
   * @param name 平台名称
   */
  usePlatform(name: string): void {
    if (!this.platforms.has(name)) {
      throw new Error(`Platform ${name} is not registered`);
    }
    this.currentPlatform = name;
  }

  /**
   * 获取当前平台实例
   * @returns 当前平台实例
   */
  private getCurrentPlatform(): IConnectCodePlatform {
    if (!this.currentPlatform || !this.platforms.has(this.currentPlatform)) {
      throw new Error('No platform selected');
    }
    return this.platforms.get(this.currentPlatform)!;
  }

  /**
   * 获取手机号码
   * @param projectId 项目ID
   * @returns 可用的手机号码
   */
  async getPhone(projectId: string): Promise<string> {
    return this.getCurrentPlatform().getPhone(projectId);
  }

  /**
   * 获取短信内容
   * @param phone 手机号码
   * @param projectId 项目ID
   * @returns 短信验证码内容
   */
  async getSmsContent(phone: string, projectId: string): Promise<string> {
    return this.getCurrentPlatform().getSmsContent(phone, projectId);
  }

  /**
   * 释放手机号码
   * @param phone 手机号码
   * @param projectId 项目ID
   */
  async releasePhone(phone: string, projectId: string): Promise<void> {
    return this.getCurrentPlatform().releasePhone(phone, projectId);
  }

  /**
   * 加黑手机号码
   * @param phone 手机号码
   * @param projectId 项目ID
   */
  async addBlacklist(phone: string, projectId: string): Promise<void> {
    return this.getCurrentPlatform().addBlacklist(phone, projectId);
  }
}